Tip Corner - a SUB for Resizehandler

level: any

by Alyce Watson [http://alycesrestaurant.com/]


Liberty BASIC has long included a handler for the resize event, which happens when a user resizes a window. The window must be of type "window" and it must not have the "_nf" style. If there is no sizing frame, as with the "_nf" style, the window cannot be resized by the user.

The resizehandler command specifies an event handler for the resize event. After a resize event, the size of the window's workspace, called the Client Area, is contained in the variables WindowWidth and WindowHeight. We can use these dimensions to resize and relocate controls so that they remain visible on a window and fill it properly. We most often see a branch label as the event handler for resizehandler.

Why use a Sub instead of a Branch Label?

Did you know that branch labels in the main program are only visible in the main program? They are not visible inside of subs and functions. Likewise, branch labels inside of subs and functions are not visible in the main program. They have separate memory spaces and cannot "see" one another's memory. If the user tries to resize a window while code is executing within a sub or function, the program will close with an error, because the designated branch label for the resize event is not visible to the sub or function.

If we use a SUB instead of a BRANCH LABEL to handle events, the event handler is visible in ALL parts of the program. If we use a SUB handler for the resizehandler, the user can resize the window while the program is executing code in the main body of code, or if it is executing a sub or function. No errors!

To use a branch label, the code looks like this:

#1 "resizehandler [resizeWindow]"

The program executes the code at the [resizeWindow] branch when the user resizes the window. To use a SUB instead:

#1 "resizehandler resizeWindow"

Just use the SUB name in the resizehandler commmand. Easy!

The Handle Variable

When we use a SUB to handle events, certain variables are passed into the sub. In the case of resizehandler and also of trapclose, the handle of the window is passed into the sub. We must set up a string variable to contain this handle. If our sub is called "DoResize" it must be declared something like this:

SUB DoResize hdle$

We can use any variable name for the handle, as long as it is a valid variable name. It must begin with an alpha character and contain only letters and numbers. It cannot be a Liberty BASIC reserved word, like PRINT. String variables must end with the $ character.

To access the handle variable within the sub, in order to send commands to the window, we prepend the # character to it. A handle string called "hdle$" would be accessed like this: "#hdle$". To give a "refresh" command to a window whose handle is passed as "hdle$", we do this:

#hdle$ "refresh"

Refreshing!

Don't forget to issue that refresh command to the window after moving and resizing the controls. This command causes Liberty BASIC to redraw the window to display the changes.

Trapclose, too!

This method of using a SUB for an event handler works for trapclose as well. Issue the trapclose command like so:

#1 "trapclose Quit"

The "Quit" SUB looks like this:

Sub Quit handle$
    close #handle$
    end
end sub

DEMO

nomainwin
textbox #1.t, 10,10,100,30
open "Resize Me to Center the Textbox!" for window as #1
#1 "resizehandler resizeMe"
#1 "trapclose Quit"
wait

Sub Quit handle$
    close #handle$
    end
end sub

Sub resizeMe handle$
    'width of textbox is 100, height is 30
    x = int((WindowWidth - 100)/2)
    y = int((WindowHeight - 30)/2)
    #1.t "!locate ";x;" ";y;" 100 30"
    #handle$ "refresh"
end sub 


Home

Wire 1.0

Gif Viewer

Report Generation

Flow Charting

Stylebits Corner

Tip Corner

API Corner

CodeAChrome

Sprite Byte

Control Panel Applets

HTTPS Data

Eddie

Submissions

Index